<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
    clear: left;
  }
  .task-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .img{
   width: 150px;
   height: 100px;
   float: left;
   margin: 5px;
  }
  .add-btn {
    float: left;
    margin-bottom: 10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>任务列表</span>
    </div>
    <div class="line"></div>
    <task-list-form
      :title="title"
      class="task-form"
      @on-success-valid="handleSubmit">
    </task-list-form>
    <Button
      class="add-btn"
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加任务
    </Button>
    <task-list-edit-modal
      v-if="modalState"
      :modalState="modalState"
      @edit-task-list-valid="handleEdit">
    </task-list-edit-modal>
    <task-list-confirm-modal
      :confirmData="confirmData"
      @confirm-task-list-valid="handleConfirm">
    </task-list-confirm-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TaskListForm from '_c/task-list-form'
import TaskListEditModal from '_c/task-list-edit-modal'
import TaskListConfirmModal from '_c/task-list-confirm-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    TaskListForm,
    TaskListEditModal,
    TaskListConfirmModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '任务管理任务列表',
      confirmData: {},
      postData: {},
      playload: null,
      row: null,
      rows: 20,
      loading: true,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 60,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '任务名称',
          key: 'name',
          align: 'center',
          minWidth: 180,
          render: (h, params) => {
            if (params.row.name && params.row.name.length > 8) {
              return h('span', {
                attrs: {
                  title: params.row.name
                }
              }, params.row.name ? params.row.name.slice(0, 8) + '...' : '-')
            } else {
              return h('span', params.row.name ? params.row.name : '-')
            }
          }
        },
        {
          title: '关联内容标题',
          key: 'cname',
          align: 'center',
          minWidth: 180,
          render: (h, params) => {
            if (params.row.cname && params.row.cname.length > 8) {
              return h('span', {
                attrs: {
                  title: params.row.cname
                }
              }, params.row.cname ? params.row.cname.slice(0, 8) + '...' : '-')
            } else {
              return h('span', params.row.cname ? params.row.cname : '-')
            }
          }
        },
        {
          title: '结算方式',
          key: 'type',
          minWidth: 180,
          align: 'center',
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.type === '3' ? '口碑结算' : params.row.type === '1' ? '到店结算' : params.row.type === '2' ? '成交结算' : params.row.type === '4' ? '销售素材' : '-'
            )
          }
        },
        {
          title: '任务设置积分',
          key: 'taskmoney',
          align: 'center',
          minWidth: 180,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.type === '3' ? `${params.row.permoney}分` : params.row.type === '1' ? `${params.row.permoney}分` : params.row.type === '2' ? `${params.row.perrate}%` : '-'
            )
          }
        },
        {
          title: '创建时间',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '活动时间',
          key: 'time',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.btime ? `${this.$moment(params.row.btime).format('YYYY/MM/DD')} ~ ${this.$moment(params.row.etime).format('YYYY/MM/DD')}` : '-')
          }
        },
        // {
        //   title: '版页类型',
        //   key: 'type',
        //   width: 150,
        //   align: 'center'
        // },
        {
          title: '排序',
          key: 'sort',
          align: 'center',
          minWidth: 150
        },
        {
          title: '状态',
          key: 'state',
          align: 'center',
          minWidth: 150,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.state === 1 ? '启动' : '关闭'
            )
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 179,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '编辑'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  ghost: 'ghost',
                  disabled: params.row.state === 0
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClose(params.row.id, params.row)
                  }
                }
              }, '关闭')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      taskList: state => state.taskList.taskList,
      dataCount: state => state.taskList.taskListDataCount,
      historyData: state => state.taskList.taskListHistoryData,
      pageSize: state => state.taskList.taskListPageSize,
      currentPage: state => state.taskList.currentPage,
      modalState: state => state.modal.modalState
    })
  },
  methods: {
    ...mapActions([
      'getTaskList',
      'getH5List',
      'getVideoList',
      'getGraphicList',
      'getTaskListCurrentRow',
      'editTaskListData',
      'updateModalState',
      'updateConfirmModalState',
      'changeTaskListPage',
      'changeTaskListPageSize',
      'changeImgPage',
      'changeCurrentPage'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      this.getTaskList(playload).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (id, row) {
      // console.log(row)
      this.row = row
      // console.log(this.row)
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getTaskListCurrentRow(newRow)
      this.updateModalState(true)
    },
    handleEdit (playload, id) {
      if (!playload.des && playload.type !== '4') {
        this.$Message.warning('请编辑任务要求！')
        return false
      }
      // console.log(playload)
      this.confirmData = playload
      this.postData = {
        id: playload.id,
        cid: playload.cid,
        // time: '',
        // ctime: '',
        btime: playload.btime ? this.$moment(playload.btime).format('YYYY-MM-DD') : '',
        etime: playload.btime ? this.$moment(playload.etime).format('YYYY-MM-DD') : '',
        type: playload.type ? parseInt(playload.type) : '',
        name: playload.name,
        des: playload.des,
        budget: playload.budget ? playload.budget : '',
        // traffic: playload.traffic,
        // contentType: playload.contentType,
        // sticky: playload.sticky,
        // openState: playload.openState,
        // state: playload.state,
        permoney: playload.permoney ? playload.permoney : '',
        perrate: playload.perrate,
        sort: playload.sort ? parseInt(playload.sort) : '',
        subtitle: playload.subtitle,
        top: playload.top ? playload.top : false,
        img: playload.img ? playload.img : '',
        state: playload.state
      }
      // console.log(this.postData)
      // console.log(this.confirmData)
      // console.log(this.postData)
      this.updateModalState(false)
      this.updateConfirmModalState(true)
    },
    async handleConfirm () {
      this.loading = true
      const resp = await this.editTaskListData(this.postData)
      if (resp.data.state === '000') {
        this.$Message.success('编辑成功')
        this.getTaskList({
          page: this.page,
          rows: this.rows,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateConfirmModalState(false)
      if (this.confirmData.currentIndex !== '' && (this.confirmData.currentImg[this.confirmData.currentIndex] !== '' || this.confirmData.currentTw[this.confirmData.currentIndex] !== '')) {
        this.confirmData.currentImg[this.confirmData.currentIndex].className = 'img'
        this.confirmData.currentTw[this.confirmData.currentIndex].className = 'img'
      }
      // this.confirmData.taskListEditModal.resetFields()
      // this.confirmData.currentPage = 1
      // this.changeCurrentPage(this.confirmData.currentPage)
      // console.log(this.currentPage)
      // this.changeImgPage(1)
    },
    async handleClose (id) {
      this.loading = true
      const resp = await this.editTaskListData({
        id: id,
        state: 0
      })
      if (resp.data.state === '000') {
        this.$Message.success('关闭成功')
        this.getTaskList({
          page: this.page,
          rows: this.rows,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error('关闭失败')
      }
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeTaskListPage(index)
      this.getTaskList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeTaskListPageSize(value)
      this.changePage(this.page)
      this.getTaskList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getTaskList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
    this.getH5List({
      type: 0,
      rows: 8
    })
    this.getGraphicList({
      type: 1,
      rows: 8
    })
    this.getVideoList({
      type: 2,
      rows: 8
    })
  }
}
</script>
